package com.book.shelves.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.shelves.entity.BookStore;
import com.book.shelves.mapper.BookStoreMapper;
import com.book.shelves.service.BookStoreService;
import com.book.common.entity.QueryRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class BookStoreServiceImpl extends ServiceImpl<BookStoreMapper, BookStore> implements BookStoreService {

    @Autowired
    BookStoreMapper bookStoreMapper;

    @Override
    public List<BookStore> findBookStore(BookStore bookStore) {
        QueryWrapper<BookStore> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(bookStore.getBreakageid())) {
            queryWrapper.eq("breakageid", bookStore.getBreakageid());
        }
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<BookStore> pageBookStore(QueryRequest request, BookStore bookStore) {
        QueryWrapper<BookStore> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(bookStore.getBookname())) {
            queryWrapper.like("bookname", bookStore.getBookname());
        }
        if (StringUtils.isNotEmpty(bookStore.getType())) {
            queryWrapper.eq("type", bookStore.getType());
        }
        if (StringUtils.isNotEmpty(bookStore.getFlag())) {
            queryWrapper.eq("flag", bookStore.getFlag());
        }
        Page<BookStore> bookStoreIPage = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(bookStoreIPage, queryWrapper);
    }

    @Override
    public void addBookStore(BookStore bookStore) {
        this.save(bookStore);
    }

    @Override
    public void updateBookStore(BookStore bookStore) {
        this.saveOrUpdate(bookStore);
    }

    @Override
    public void deleteBookStore(String[] bookStore) {
        List<String> list = Arrays.asList(bookStore);
        this.removeByIds(list);
    }

    @Override
    public List<BookStore> askBookStore(String[] bookStore) {
        List<String> list = Arrays.asList(bookStore);
        return (List<BookStore>) this.listByIds(list);
    }
}